layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoPhotoAlbum = {
        id: 'BoPhotoAlbumTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'name', title: '相册名称', align: 'center'},
            {field: 'type', title: '类型', align: 'center',
                templet: function (object) {
                    if(object.type == 0){
                        return '<span class="layui-badge layui-bg-green">照片</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">视频</span>';
                    }
                }
            },
            {field: 'content', title: '简介', align: 'center',
                templet: function (object) {
                    if(object.content == '' || object.content == null){
                        return '-';
                    }else{
                        return object.content;
                    }
                }
            },
            {field: 'isLock', title: '是否加锁', align: 'center', width: 100,
                templet: function (object) {
                    if(object.isLock == 0){
                        return '<input type="checkbox" lay-filter="l_isLockSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '">';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isLockSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '" checked>';
                    }
                }
            },
            {field: 'password', title: '密码', align: 'center',
                templet: function (object) {
                    if(object.password == '' || object.password == null){
                        return '-';
                    }else{
                        return object.password;
                    }
                }
            },
            {field: 'isShow', title: '是否显示', align: 'center', width: 100,
                templet: function (object) {
                    if(object.isShow == 0){
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '" checked>';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isShowSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '">';
                    }
                }
            },
            {field: 'readNum', title: '阅读数量', align: 'center', width: 100},
            {field: 'praiseNum', title: '赞数量', align: 'center', width: 100},
            {field: 'img', title: '相册封面图', align: 'center',
                templet: function (object) {
                    var img = '<img src="/photo_album_img/' + object.img +
                        '" lay-event="previewImg"' + ' style="cursor: pointer;max-width:100px; max-height:100px;"';
                    return img;
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {field: 'createDate', title: '创建时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoPhotoAlbum.id,
        url: '/web/boPhotoAlbum/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var name = $("#search_name").val();
        var type = $("#search_type").val();

        //封装
        var dataObject = {};
        dataObject['name'] = name;
        dataObject['type'] = type;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPhotoAlbum.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_name").val("");
        $("#search_type").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['name'] = "";
        dataObject['type'] = "-1";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPhotoAlbum.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });


    // 监听添加操作
    $(".layuimini-container").on("click", "#add-btn", function () {
        var content = layuimini.getHrefContent('/web/boPhotoAlbum/addOrEdit');
        var index = layer.open({
            title: '添加相册',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['600px','600px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoPhotoAlbum.id);
            }
        });

        return true;
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    BoPhotoAlbum.edit = function(id){
        var content = layuimini.getHrefContent('/web/boPhotoAlbum/addOrEdit?id=' + id);
        var index = layer.open({
            title: '编辑相册',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['600px','600px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoPhotoAlbum.id);
            }
        });

        return false;
    };

    /**
     * 点击删除按钮
     * @param id
     */
    BoPhotoAlbum.delete = function(id){
        layer.open({
            title: '删除相册'
            ,content: '是否确定删除该相册？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boPhotoAlbum/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoPhotoAlbum.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    //监听是否加锁开关
    form.on('switch(l_isLockSwitch)', function(data){
        var isLock = 0;
        if(data.elem.checked){
            isLock = 1;
        }

        var photoAlbumId = data.value; //编号

        //更新状态
        var indexLoad = layer.load(2);
        var url = '/web/boPhotoAlbum/updateIsLock';
        $.post(url, {isLock: isLock, photoAlbumId: photoAlbumId}, function(data){
            layer.close(indexLoad);
            if(data.state == "200"){
                layer.msg(data.message, {icon: 1}); //涉及修改密码刷新
                table.reload(BoPhotoAlbum.id);
            }else{
                layer.msg(data.message, {icon: 2});
                //更新失败则改状态
                if(data.elem.checked){
                    data.elem.checked = false;
                }else{
                    data.elem.checked = true;
                }
                form.render();
            }
        })
    });

    //监听是否显示开关
    form.on('switch(l_isShowSwitch)', function(data){
        var isShow = 1;
        if(data.elem.checked){
            isShow = 0;
        }

        var photoAlbumId = data.value; //编号

        //更新状态
        var indexLoad = layer.load(2);
        var url = '/web/boPhotoAlbum/updateIsShow';
        $.post(url, {isShow: isShow, photoAlbumId: photoAlbumId}, function(data){
            layer.close(indexLoad);
            if(data.state == "200"){
                layer.msg(data.message, {icon: 1});
            }else{
                layer.msg(data.message, {icon: 2});
                //更新失败则改状态
                if(data.elem.checked){
                    data.elem.checked = false;
                }else{
                    data.elem.checked = true;
                }
                form.render();
            }
        })
    });

    //封面图预览
    BoPhotoAlbum.previewImg = function(data) {

        var width = (window.screen.width - 400) + 'px';
        var height = (window.screen.height - 300) + 'px';

        var imgHtml = "<img src='/photo_album_img/" + data.img + "' style='min-width:100px; min-height:100px;max-width:" + width + ";max-height:" + height + ";'/>";
        //捕获页
        var index = layer.open({
            type: 1,
            title: false, //不显示标题
            shadeClose: true,
            skin: 'previewImg-class', //透明
            area: ['auto', 'auto'],
            content: imgHtml //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        });

    };

    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoPhotoAlbum.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            BoPhotoAlbum.delete(data.id);
        } else if (layEvent === 'edit') {
            BoPhotoAlbum.edit(data.id)
        } else if(layEvent === 'previewImg'){
            BoPhotoAlbum.previewImg(data);
        }
    });

});